package com.example.test84;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddBookActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHeaper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book);

    }

    @Override
    protected void onResume() {
        super.onResume();
        setContentView(R.layout.activity_add_book);
        dbHeaper = new MyDatabaseHelper(this,"BookStore2.db",null,2);
        SQLiteDatabase db = dbHeaper.getWritableDatabase();
        ContentValues values = new ContentValues();
        //获取输入框组件
        EditText name_edit =  (EditText) findViewById(R.id.name_edit);
        EditText author_edit =  (EditText) findViewById(R.id.author_edit);
        EditText price_edit =  (EditText) findViewById(R.id.price_edit);
        EditText pages_edit =  (EditText) findViewById(R.id.pages_edit);
        EditText category_id_edit =  (EditText) findViewById(R.id.category_id_edit);


        Button add = (Button)findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入款的内容
                String name = name_edit.getText().toString();
                String author = author_edit.getText().toString();
                String price = price_edit.getText().toString();
                String pages = pages_edit.getText().toString();
                String category_id = category_id_edit.getText().toString();
                //表：向Book表添加数据
                values.put("name",name);
                values.put("author",author);
                values.put("pages",price);
                values.put("price",pages);
                values.put("category_id",category_id);
                db.insert("Book",null,values);
                values.clear();

                Intent intent = new Intent(AddBookActivity.this,MainActivity.class);
                startActivity(intent);

            }
        });
    }
}